package com.xjcs.saplingplan.ui.activity.countdown

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.jeremyliao.liveeventbus.LiveEventBus
import com.xjcs.saplingplan.base.BaseVbActivity
import com.xjcs.saplingplan.const.EventBusConstants
import com.xjcs.saplingplan.const.setSingleClick
import com.xjcs.saplingplan.const.toToast
import com.xjcs.saplingplan.databinding.ActivityCountdownCateCreateBinding
import com.xjcs.saplingplan.datastore.AppData
import com.xjcs.saplingplan.room.bean.countdown.PlanCateBean
import com.xjcs.saplingplan.room.utils.PlanCateRoomUtils

class CountdownCateCreateActivity : BaseVbActivity<ActivityCountdownCateCreateBinding>() {

    private val userId = AppData.getUserId()

    companion object {
        fun startActivity(context: Context) {
            val intent = Intent(context, CountdownCateCreateActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getViewBinding(): ActivityCountdownCateCreateBinding {
        return ActivityCountdownCateCreateBinding.inflate(layoutInflater)
    }

    override fun initUi(savedInstanceState: Bundle?) {
    }

    override fun registerObserver() {
    }

    override fun initListener() {
        mBinding.ivBack.setSingleClick {
            finish()
        }
        mBinding.tvSave.setSingleClick {
            val cateName = mBinding.etInput.text.toString().trim()
            if (cateName.isEmpty()) {
                "请输入分类名称".toToast()
                return@setSingleClick
            }
            val cateBean = PlanCateRoomUtils.getPlanCateBean(cateName)
            if(cateBean != null){
                "该分类已存在".toToast()
            }else{
                PlanCateRoomUtils.insert(PlanCateBean().apply {
                    planCateUserId = userId
                    planCateName = cateName
                    planCateSort = PlanCateRoomUtils.getCateSortMax(userId)      //排序序号
                })
                LiveEventBus.get<String>(EventBusConstants.REFRESH_COUNTDOWN_CATE).post("")
                finish()
            }
        }

    }
}